import 'package:date_format/date_format.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

// class CustomTimePicker extends StatefulWidget {
//   final Function onTimeSelected;
//   final int timeType; //0 day 1 月 2 年
//   final DateTime? now;
//   const CustomTimePicker({super.key, required this.onTimeSelected, required this.timeType, this.now});
//
//   @override
//   State<CustomTimePicker> createState() => _CustomTimePickerState();
// }

// class _CustomTimePickerState extends State<CustomTimePicker> {
//
//   FixedExtentScrollController controller =  FixedExtentScrollController(initialItem: 0);
//   late DateTime now;
//
//   List months = [
//     {
//       'name': 'Jan',
//       'value': '01'
//     },
//     {
//       'name': 'Feb',
//       'value': '02'
//     },
//     {
//       'name': 'Mar',
//       'value': '03'
//     },
//     {
//       'name': 'Apr',
//       'value': '04'
//     },
//     {
//       'name': 'May',
//       'value': '05'
//     },
//     {
//       'name': 'Jun',
//       'value': '06'
//     },
//     {
//       'name': 'Jul',
//       'value': '07'
//     },
//     {
//       'name': 'Aug',
//       'value': '08'
//     },
//     {
//       'name': 'Sep',
//       'value': '09'
//     },
//     {
//       'name': 'Oct',
//       'value': '10'
//     },
//     {
//       'name': 'Nov',
//       'value': '11'
//     },
//     {
//       'name': 'Dec',
//       'value': '12'
//     }
//   ];
//
//   Map selMap = {};
//
//   List values = [];
//
//   @override
//   void didChangeDependencies() {
//     // TODO: implement didChangeDependencies
//     super.didChangeDependencies();
//     now = widget.now ?? DateTime.now();
//     if(widget.timeType == 1){
//       String month = formatDate(now, [mm]);
//       for(int i=0; i<months.length; i++){
//         DateTime start = DateTime(now.year, int.parse(months[i]['value']), 1);
//         DateTime end = DateTime(now.year, int.parse(months[i]['value'])+1, 0);
//         months[i]['start'] = formatDate(start, [yyyy,'-',mm,'-',dd]);
//         months[i]['end'] = formatDate(end, [yyyy,'-',mm,'-',dd]);
//         if(month == months[i]['value']){
//           controller = FixedExtentScrollController(initialItem: i);
//           if(widget.onTimeSelected!=null){
//             widget.onTimeSelected(months[i]);
//           }
//         }
//       }
//     }else if(widget.timeType == 0){
//       int days = DateTime(now.year, now.month+1, 0).day;
//       for(int i=1; i<=days; i++){
//         values.add('$i');
//         if(i == now.day){
//           controller = FixedExtentScrollController(initialItem: i);
//           if(widget.onTimeSelected!=null){
//             widget.onTimeSelected(values[i]);
//           }
//         }
//       }
//     }else if(widget.timeType == 2){
//       int index = 0;
//       print('year === ${now.year}');
//       for(int i=2000; i<=now.year+100; i++){
//         values.add('$i');
//         if(i == now.year){
//           controller = FixedExtentScrollController(initialItem: index);
//           if(widget.onTimeSelected!=null){
//             widget.onTimeSelected(values[index]);
//           }
//         }
//         index++;
//       }
//     }
//     if(mounted){setState(() {});}
//   }
//
//   @override
//   void initState() {
//     // TODO: implement initState
//     super.initState();
//     now = widget.now ?? DateTime.now();
//     if(widget.timeType == 1){
//       String month = formatDate(now, [mm]);
//       for(int i=0; i<months.length; i++){
//         DateTime start = DateTime(now.year, int.parse(months[i]['value']), 1);
//         DateTime end = DateTime(now.year, int.parse(months[i]['value'])+1, 0);
//         months[i]['start'] = formatDate(start, [yyyy,'-',mm,'-',dd]);
//         months[i]['end'] = formatDate(end, [yyyy,'-',mm,'-',dd]);
//         if(month == months[i]['value']){
//           controller = FixedExtentScrollController(initialItem: i);
//           if(widget.onTimeSelected!=null){
//             widget.onTimeSelected(months[i]);
//           }
//         }
//       }
//     }else if(widget.timeType == 0){
//       int days = DateTime(now.year, now.month+1, 0).day;
//       for(int i=1; i<=days; i++){
//         values.add('$i');
//         if(i == now.day){
//           controller = FixedExtentScrollController(initialItem: i);
//           if(widget.onTimeSelected!=null){
//             widget.onTimeSelected(values[i]);
//           }
//         }
//       }
//     }else if(widget.timeType == 2){
//       int index = 0;
//       print('year === ${now.year}');
//       for(int i=2000; i<=now.year+100; i++){
//         values.add('$i');
//         if(i == now.year){
//           controller = FixedExtentScrollController(initialItem: index);
//           if(widget.onTimeSelected!=null){
//             widget.onTimeSelected(values[index]);
//           }
//         }
//         index++;
//       }
//     }
//   }
//
//
// }

class CustomTimePickerM extends StatelessWidget {
  final Function onTimeSelected;
  final int timeType; //0 day 1 月 2 年
  final DateTime? now;
  final List values;
  final int selIndex;
  const CustomTimePickerM({super.key, required this.onTimeSelected, required this.timeType, this.now, required this.values, required this.selIndex,});

  @override
  Widget build(BuildContext context) {
    return CupertinoPicker(
      itemExtent: 120.w,
      magnification: 1,
      offAxisFraction: 0,
      useMagnifier: true,
      diameterRatio: 1,
      scrollController: FixedExtentScrollController(initialItem: selIndex),
      onSelectedItemChanged: (index){
        onTimeSelected(index,values[index]);
      },
      selectionOverlay: Container(
        decoration: BoxDecoration(
            border: Border(top: BorderSide(color: Color.fromRGBO(255, 255, 255, 0.1)),bottom: BorderSide(color: Color.fromRGBO(255, 255, 255, 0.1)))
        ),
      ),
      children: _monthWidget(),
    );
  }

  List<Widget> _monthWidget(){
    List<Widget> arr = [];
    for(String item in values){
      arr.add(Container(
        height: 120.w,
        alignment: Alignment.center,
        decoration: BoxDecoration(
        ),
        child: Text(item,style: TextStyle(
            color: Colors.white,
            fontSize: 48.sp,
            fontWeight: FontWeight.w400
        ),),
      ));
    }
    return arr;
  }
}

class CustomTimePickerY extends StatelessWidget {
  final Function onTimeSelected;
  final int timeType; //0 day 1 月 2 年
  final DateTime? now;
  final List values;
  final int selIndex;
  const CustomTimePickerY({super.key, required this.onTimeSelected, required this.timeType, this.now, required this.values, required this.selIndex});

  @override
  Widget build(BuildContext context) {
    print('选择的MMMindex==== $selIndex');
    return CupertinoPicker(
      itemExtent: 120.w,
      magnification: 1,
      offAxisFraction: 0,
      useMagnifier: true,
      diameterRatio: 1,
      scrollController: FixedExtentScrollController(initialItem: selIndex),
      onSelectedItemChanged: (index){
        onTimeSelected(index,values[index]);
      },
      selectionOverlay: Container(
        decoration: BoxDecoration(
            border: Border(top: BorderSide(color: Color.fromRGBO(255, 255, 255, 0.1)),bottom: BorderSide(color: Color.fromRGBO(255, 255, 255, 0.1)))
        ),
      ),
      children: _monthWidget(),
    );
  }

  List<Widget> _monthWidget(){
    List<Widget> arr = [];
    for(String item in values){
      arr.add(Container(
        height: 120.w,
        alignment: Alignment.center,
        decoration: BoxDecoration(
        ),
        child: Text(item,style: TextStyle(
            color: Colors.white,
            fontSize: 48.sp,
            fontWeight: FontWeight.w400
        ),),
      ));
    }
    return arr;
  }
}

class CustomTimePickerYMDY extends StatelessWidget {
  final Function onTimeSelected;
  final int timeType; //0 day 1 月 2 年
  final DateTime? now;
  final List values;
  final int selIndex;
  const CustomTimePickerYMDY({super.key, required this.onTimeSelected, required this.timeType, this.now, required this.values, required this.selIndex});

  @override
  Widget build(BuildContext context) {
    print('选择的YYYindex==== $selIndex');
    FixedExtentScrollController scrollController = FixedExtentScrollController(initialItem: selIndex);
    Future.delayed(Duration(milliseconds: 600),(){
      scrollController.jumpToItem(selIndex);
    });
    return CupertinoPicker(
      itemExtent: 120.w,
      magnification: 1,
      offAxisFraction: 0,
      useMagnifier: true,
      diameterRatio: 1,
      scrollController: scrollController,
      onSelectedItemChanged: (index){
        onTimeSelected(index,values[index]);
      },
      selectionOverlay: Container(
        decoration: BoxDecoration(
            border: Border(top: BorderSide(color: Color.fromRGBO(255, 255, 255, 0.1)),bottom: BorderSide(color: Color.fromRGBO(255, 255, 255, 0.1)))
        ),
      ),
      children: _monthWidget(),
    );
  }

  List<Widget> _monthWidget(){
    List<Widget> arr = [];
    for(String item in values){
      arr.add(Container(
        height: 120.w,
        alignment: Alignment.center,
        decoration: BoxDecoration(
        ),
        child: Text(item,style: TextStyle(
            color: Colors.white,
            fontSize: 48.sp,
            fontWeight: FontWeight.w400
        ),),
      ));
    }
    return arr;
  }
}

class CustomTimePickerYMDM extends StatelessWidget {
  final Function onTimeSelected;
  final int timeType; //0 day 1 月 2 年
  final DateTime? now;
  final List values;
  final int selIndex;
  const CustomTimePickerYMDM({super.key, required this.onTimeSelected, required this.timeType, this.now, required this.values, required this.selIndex});

  @override
  Widget build(BuildContext context) {
    print('选择的MMMMindex==== $selIndex');
    FixedExtentScrollController scrollController = FixedExtentScrollController(initialItem: selIndex);
    Future.delayed(Duration(milliseconds: 600),(){
      scrollController.jumpToItem(selIndex);
    });
    return CupertinoPicker(
      itemExtent: 120.w,
      magnification: 1,
      offAxisFraction: 0,
      useMagnifier: true,
      diameterRatio: 1,
      scrollController: scrollController,
      onSelectedItemChanged: (index){
        onTimeSelected(index,values[index]);
      },
      selectionOverlay: Container(
        decoration: BoxDecoration(
            border: Border(top: BorderSide(color: Color.fromRGBO(255, 255, 255, 0.1)),bottom: BorderSide(color: Color.fromRGBO(255, 255, 255, 0.1)))
        ),
      ),
      children: _monthWidget(),
    );
  }

  List<Widget> _monthWidget(){
    List<Widget> arr = [];
    for(String item in values){
      arr.add(Container(
        height: 120.w,
        alignment: Alignment.center,
        decoration: BoxDecoration(
        ),
        child: Text(item,style: TextStyle(
            color: Colors.white,
            fontSize: 48.sp,
            fontWeight: FontWeight.w400
        ),),
      ));
    }
    return arr;
  }
}

class CustomTimePickerYMDD extends StatelessWidget {
  final Function onTimeSelected;
  final int timeType; //0 day 1 月 2 年
  final DateTime? now;
  final List values;
  final int selIndex;
  const CustomTimePickerYMDD({super.key, required this.onTimeSelected, required this.timeType, this.now, required this.values, required this.selIndex});

  @override
  Widget build(BuildContext context) {
    print('选择的DDDindex==== $selIndex');
    FixedExtentScrollController scrollController = FixedExtentScrollController(initialItem: selIndex);
    Future.delayed(Duration(milliseconds: 600),(){
      scrollController.jumpToItem(selIndex);
    });
    return CupertinoPicker(
      itemExtent: 120.w,
      magnification: 1,
      offAxisFraction: 0,
      useMagnifier: true,
      diameterRatio: 1,
      scrollController: scrollController,
      onSelectedItemChanged: (index){
        onTimeSelected(index,values[index]);
      },
      selectionOverlay: Container(
        decoration: BoxDecoration(
            border: Border(top: BorderSide(color: Color.fromRGBO(255, 255, 255, 0.1)),bottom: BorderSide(color: Color.fromRGBO(255, 255, 255, 0.1)))
        ),
      ),
      children: _monthWidget(),
    );
  }

  List<Widget> _monthWidget(){
    List<Widget> arr = [];
    for(String item in values){
      arr.add(Container(
        height: 120.w,
        alignment: Alignment.center,
        decoration: BoxDecoration(
        ),
        child: Text(item,style: TextStyle(
            color: Colors.white,
            fontSize: 48.sp,
            fontWeight: FontWeight.w400
        ),),
      ));
    }
    return arr;
  }
}
